package lib.manager;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import lib.manager.dao.UserDao;
import lib.manager.model.User;



@Controller
@SpringBootApplication
public class App 
{
	@Autowired
	private UserDao userDao;
	
	private static Map<String, Date> tokenMap = new HashMap<String, Date>();//TODO:use db
	
	private final long ONE_WEEK_MILLSECONDS = 1000 * 60 * 60 * 24 *7;
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	String login(String username, String password){
		if("username".equals(username) && "password".equals(password)){
			long now = new Date().getTime();
			String token = username + '|' +   now;//unsafe
			Date weekLater = new Date(now + ONE_WEEK_MILLSECONDS);
			
			tokenMap.put(token, weekLater);
			return token;
		}
		
		return "LOGIN FAILS";
	}
	
	@RequestMapping("/logout")
	@ResponseBody
	String logout(String token){
		if(this.isAuthorized(token)){
			tokenMap.remove(token);
		}
		return "LOGOUT SUCCEED";
	}
	
	@RequestMapping(value="/auth/{token}",method=RequestMethod.GET)
	@ResponseBody
	Boolean isAuthorized(@RequestParam("token")String token){
		if(token == null || "".equals(token))
			return false;
		
		if(tokenMap.containsKey(token)){
			return true;
		}
		return false;
	}
	
	@RequestMapping(value="/users")
	@ResponseBody
	Object getUsers(String token, int page){
		if(!this.isAuthorized(token)){
			return "UNAUTHORIZED";
		}
		
		PageRequest paging = new PageRequest(page, 20, Direction.DESC, "registerTime");
		return userDao.findAll(paging);
	}
	
	@RequestMapping(value="/user")
	@ResponseBody
	Object findById(String token, Long id){
		if(!this.isAuthorized(token)){
			return "UNAUTHORIZED";
		}
		return userDao.findOne(id);
	}
	
	@RequestMapping(value="/addUser",method=RequestMethod.POST)
	@ResponseBody
	String addUser(String token,String username, String password, String email){
		if(!this.isAuthorized(token)){
			return "UNAUTHORIZED";
		}
		if(username == null || "".equals(username)){
			return "User is required";
		}
		if(password == null || "".equals(password)){
			return "password is required";
		}
		if(email == null || "".equals(email)){
			return "Email is required";
		}
		
		User user = new User();
		user.setUsername(username);;
		user.setPassword(password);//Require encryption
		user.setEmail(email);
		userDao.save(user);
		return "User added";
	}
	
	@RequestMapping("/delUser")
	@ResponseBody
	String deleteUser(String token, Long id){
		if(!this.isAuthorized(token)){
			return "UNAUTHORIZED";
		}
		if(id != null)
			userDao.delete(id);
		return "User Deleted";
	}
	
	
	@ExceptionHandler(value=Exception.class)
	@ResponseBody
	String responseError(Exception e){
		return "Error";
	}
	
	
    public static void main( String[] args )
    {
    	SpringApplication.run(App.class, args);
    }
}
